<template>
	<view class="content">
		<u-navbar title="权限管理" :border-bottom="false" :background="{background:'#FAFAFA'}"></u-navbar>
		<view class="content_main">
			<view class="content_main_tip">
				为了向您提供更好的用户体验，会在特定场景可能需要向您申请一下手机权限
			</view>
			<view class="content_main_item" v-for="(item,index) in data.list" :key="index"  @click="toAppSystem">
				<view class="u-flex u-row-between">
					<view class="content_main_item_tit">{{item.name}}</view>
					<view>
						{{item.check ? '已开启' : '去设置'}}
						<u-icon name="arrow-right"></u-icon>
					</view>
				</view>
				<view class="u-m-t-20">
					{{item.text}}
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	// #ifdef APP-PLUS
	import permision from "@/js_sdk/permission/permission.js"
	// #endif
	import {
		reactive
	} from "vue";
	import {
		onShow,
		onLoad
	} from '@dcloudio/uni-app'
	const data = reactive({
		list: [{
			name: '位置权限',
			text: '推荐附近职位、选择目标求职地点、添加/修改住址、设置我的住址等申请此权限',
			check: false
		}, {
			name: '相机权限',
			text: '使用二维码扫描、拍摄图片等申请此权限',
			check: false
		}, {
			name: '相册权限',
			text: '上传头像、上传作品等申请此权限',
			check: false
		}]
	})
	onLoad(() => {
		if (uni.$u.os() != 'ios') {
			data.list.push({
				name: "拨打电话权限",
				text: "拨打电话等申请此权限",
				value: false,
			})
		}
	})
	onShow(() => {
		// #ifdef APP-PLUS
		getSystemInfoSync()
		// #endif
	})
	const requestAndroidPermission = async (permisionID) => {
		var result = await permision.requestAndroidPermission(permisionID);
		return result
	}
	const getSystemInfoSync = async () => { //判断APP运行环境
		if (uni.$u.os() == "ios") {
			Promise.all([permision.judgeIosPermission("location"), permision.judgeIosPermission("photoLibrary"),
				permision.judgeIosPermission("photoLibrary")
			]).then(res => {
				data.list.map((item, index) => {
					item.check = res[index]
					return
				})
			})
		} else {
			// 1:已经有授权 0:未获取 -1:被永久拒绝授权
			Promise.all([requestAndroidPermission("android.permission.ACCESS_FINE_LOCATION"),
				requestAndroidPermission("android.permission.CAMERA"),
				requestAndroidPermission("android.permission.READ_EXTERNAL_STORAGE"),
				requestAndroidPermission("android.permission.CALL_PHONE") //拨打电话
			]).then(res => {
				data.list.map((item, index) => {
					item.check = res[index] == 1 ? true : false
					return
				})
			})
		}
	}
	// 打开APP系统设置
	const toAppSystem = () => {
		// #ifdef APP-PLUS
		permision.gotoAppPermissionSetting()
		// #endif
	}
</script>

<style>
	page {
		background-color: #FAFAFA;
	}
</style>
<style lang="scss" scoped>
	.content {
		&_main {
			padding: 0 30rpx;

			&_tip {
				color: #867f7f;
				padding: 24rpx 0;
				font-size: 24rpx;
			}

			&_item {
				background-color: #fff;
				margin-bottom: 30rpx;
				padding: 30rpx 20rpx;
				color: #BBBBBB;
				font-size: 28rpx;

				&_tit {
					font-size: 32rpx;
					color: #101010;
				}
			}
		}
	}
</style>